﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FortuneCat.WebUI.Models;
using FortuneCat.WebUI.Infrastructure.Abstract;

namespace FortuneCat.WebUI.Controllers
{
    public class AccountController : Controller
    {
        IAuthProvider _authProvider;

        public AccountController(IAuthProvider authProvider) {
            _authProvider = authProvider;
        }

        public ActionResult LogOn() {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnViewModel model, string returnUrl) {
            if (ModelState.IsValid) {
                if (_authProvider.Authenticate(model.UserName, model.Password)) {
                    return Redirect(returnUrl ?? Url.Action("Index", "Home"));
                }
                else {
                    ModelState.AddModelError("", "用户或密码不正确");
                    return View();
                }
            }
            else {
                return View();
            }
        }

        public ActionResult LogOff() {
            _authProvider.LogOff();
            return RedirectToAction("Index", "Home");
        }

    }
}
